import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:provider/provider.dart';

import '../../data/models/bt_product.dart';
import '../../widgets/pagination/bt_first_page_error_indicator.dart';
import '../../widgets/pagination/bt_first_page_progress_indicator.dart';
import '../../widgets/pagination/bt_new_page_error_indicator.dart';
import '../../widgets/pagination/bt_new_page_progress_indicator.dart';
import '../../widgets/pagination/bt_no_items_found_indicator.dart';
import '../../widgets/pagination/bt_no_more_items_indicator.dart';
import 'models/bt_discover_model.dart';
import 'widgets/bt_product_item_widget.dart';

@RoutePage(name: "discoverRoute")
class BtDiscoverPage extends StatelessWidget {
  const BtDiscoverPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Image.asset(
          width: MediaQuery.of(context).size.width,
          'assets/images/bt_discover_full_bg.png',
          fit: BoxFit.fill,
        ),
        const Positioned(
          left: 18,
          top: 65,
          child: Text('Ordering service',
              maxLines: 1,
              style: TextStyle(fontSize: 20, color: Color(0xFF202020), fontWeight: FontWeight.bold)),
        ),
        Container(
            width: MediaQuery.of(context).size.width,
            margin: const EdgeInsets.only(top: 96),
            decoration: const BoxDecoration(
                image: DecorationImage(
              image: ExactAssetImage('assets/images/bt_discover_board_bg.png'),
              fit: BoxFit.fill,
            )),
            child: Column(
              children: [
                const Padding(
                  padding: EdgeInsets.only(top: 50),
                  child: Text('Choose the items you need!',
                      textAlign: TextAlign.center,
                      maxLines: 1,
                      style: TextStyle(fontSize: 20, color: Color(0xFF2236A3), fontWeight: FontWeight.bold)),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 90),
                  child: Image.asset(
                    width: 253,
                    height: 26,
                    'assets/images/bt_goods_need_logo.png',
                  ),
                ),
                Expanded(
                  child: Container(
                    margin: const EdgeInsets.only(left: 44, top: 50, right: 48, bottom: 0),
                    child: ChangeNotifierProvider(
                      create: (context) => BtDiscoverModel(),
                      child: Consumer<BtDiscoverModel>(builder: (context, model, _) {
                        return RefreshIndicator(
                          onRefresh: () => Future.sync(
                            () => model.pagingController.refresh(),
                          ),
                          child: MediaQuery.removePadding(
                            context: context,
                            removeTop: true,
                            child: PagedListView<int, BtProduct>(
                              pagingController: model.pagingController,
                              builderDelegate: PagedChildBuilderDelegate<BtProduct>(
                                itemBuilder: (context, item, index) {
                                  return Column(
                                    children: [
                                      BtProductItemWidget(
                                        index: index,
                                        item: item,
                                        model: model,
                                      ),
                                      const SizedBox(
                                        height: 10,
                                      ),
                                    ],
                                  );
                                },
                                firstPageErrorIndicatorBuilder: (_) => BtFirstPageErrorIndicator(
                                  error: model.pagingController.error,
                                  onTryAgain: () => model.pagingController.refresh(),
                                ),
                                newPageErrorIndicatorBuilder: (_) => BtNewPageErrorIndicator(
                                  error: model.pagingController.error,
                                  onTap: () => model.pagingController.retryLastFailedRequest(),
                                ),
                                firstPageProgressIndicatorBuilder: (_) =>
                                    const BtFirstPageProgressIndicator(),
                                newPageProgressIndicatorBuilder: (_) => const Padding(
                                  padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                                  child: BtNewPageProgressIndicator(),
                                ),
                                noItemsFoundIndicatorBuilder: (_) => const Padding(
                                  padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                                  child: BtNoItemsFoundIndicator(),
                                ),
                                noMoreItemsIndicatorBuilder: (_) => const Padding(
                                  padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                                  child: BtNoMoreItemsIndicator(),
                                ),
                              ),
                            ),
                          ),
                        );
                      }),
                    ),
                  ),
                )
              ],
            )),
      ],
    );
  }
}
